package javax.generic.swing;

import java.awt.Dialog;

import javax.generic.persistance.AbstractPersistableEntity;
import javax.generic.persistance.GenericDao;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class CreateEntityForm<T extends AbstractPersistableEntity> extends VisualEditor<T> {

	private static final long serialVersionUID = -4343698595961475371L;

	private GenericDao dao;
	private JTable table;

	public CreateEntityForm(T object, GenericDao dao, JTable table, Dialog owner) {
		super(object, owner);
		this.dao = dao;
		this.table = table;
	}

	@Override
	public void onOk(T object) {
		dao.persist(object);

		DefaultTableModel dtm = (DefaultTableModel)table.getModel();
		dtm.fireTableDataChanged();

		dtm.addRow(object.getRow());
		dtm.fireTableDataChanged();

		table.setRowSelectionInterval(table.getRowCount() - 1, table.getRowCount() - 1);
		super.onOk(object);
	}
}
